import java.io.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Register implements ActionListener {
        JTextField tf = new JTextField();
        JPasswordField psf1 = new JPasswordField();
        JPasswordField psf2 = new JPasswordField();
        LoadIn p;
        JButton button = new JButton("注册");
        JLabel label1 = new JLabel("请输入账号: ");
        JLabel label2 = new JLabel("请输入密码: ");
        JLabel label3 = new JLabel("请确认密码: ");
        JLabel label4 = new JLabel("提示: 若注册成功则自动返回");

        JFrame jf = new JFrame("德州扑克 - 注册");

        public static void main(String[] args) {
                new Register(null);
        }

        public Register(LoadIn p) {
                this.p = p;
                jf.setVisible(true);
                jf.setSize(285, 190);
                jf.setResizable(false);
                jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JPanel panel = new JPanel();
                button.addActionListener(this);

                /* 注册界面GroupLayout布局 */
                GroupLayout layout = new GroupLayout(panel);
                panel.setLayout(layout);
                // 自动创建组件之间的间隙
                layout.setAutoCreateGaps(true);
                // 自动创建容器与触到容器边框的组件之间的间隙
                layout.setAutoCreateContainerGaps(true);
                // 水平组
                GroupLayout.SequentialGroup hSequentialGroup = layout.createSequentialGroup().addComponent(label1)
                                .addComponent(tf);
                GroupLayout.SequentialGroup hSequentialGroup2 = layout.createSequentialGroup().addComponent(label2)
                                .addComponent(psf1);
                GroupLayout.SequentialGroup hSequentialGroup3 = layout.createSequentialGroup().addComponent(label3)
                                .addComponent(psf2);
                GroupLayout.ParallelGroup hParallelGroup = layout.createParallelGroup().addGroup(hSequentialGroup)
                                .addGroup(hSequentialGroup2).addGroup(hSequentialGroup3);

                GroupLayout.SequentialGroup hSequentialGroup5 = layout.createSequentialGroup().addComponent(button);
                GroupLayout.SequentialGroup hSequentialGroup4 = layout.createSequentialGroup()
                                .addGroup(hSequentialGroup5).addComponent(label4);
                GroupLayout.ParallelGroup hGroup = layout.createParallelGroup().addGroup(hParallelGroup)
                                .addGroup(hSequentialGroup4);
                layout.setHorizontalGroup(hGroup);
                // 垂直组
                GroupLayout.ParallelGroup vParallelGroup = layout.createParallelGroup().addComponent(label1)
                                .addComponent(tf);
                GroupLayout.ParallelGroup vParallelGroup2 = layout.createParallelGroup().addComponent(label2)
                                .addComponent(psf1);
                GroupLayout.ParallelGroup vParallelGroup3 = layout.createParallelGroup().addComponent(label3)
                                .addComponent(psf2);
                GroupLayout.SequentialGroup vSequentialGroup = layout.createSequentialGroup().addGroup(vParallelGroup)
                                .addGroup(vParallelGroup2).addGroup(vParallelGroup3);
                GroupLayout.ParallelGroup vParallelGroup4 = layout.createParallelGroup().addComponent(button)
                                .addComponent(label4);
                GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup().addGroup(vSequentialGroup)
                                .addGroup(vParallelGroup4);
                layout.setVerticalGroup(vGroup);
                jf.setContentPane(panel);
                jf.setLocationRelativeTo(null);
                // this.pack();
        }

        public void actionPerformed(ActionEvent e) {
                if (e.getSource() == button) {
                        if (tf.getText().isEmpty()) {
                                label4.setText("用户名不能为空!");
                                return;
                        }
                        if (String.valueOf(psf1.getPassword()).isEmpty()) {
                                label4.setText("密码不能为空!");
                                return;
                        }
                        if (!String.valueOf(psf1.getPassword()).equals(String.valueOf(psf2.getPassword()))) {
                                label4.setText("两次密码不匹配!");
                                return;
                        }
                        Test.link.println("<Register>");
                        Test.link.println(tf.getText());
                        Test.link.println(String.valueOf(psf1.getPassword()));
                        try {
                                if (Test.link.readLine().equals("注册成功")) {
                                        System.out.println("注册成功");
                                } else {
                                        System.err.println("注册失败");
                                        return;
                                }
                        } catch (IOException ioe) {
                                System.err.println("服务器错误");
                                ioe.printStackTrace();
                                return;
                        }
                        p.jumpFrom(this.jf);
                }
        }
}
